﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace DAL.Product
{
    public class ProductRepository
    {
        private ImsContext _context = null;

        public ProductRepository()
        {
            _context = new ImsContext();
        }

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<BO.Product> GetAll()
        {
            //   var repository = new Repository<BO.Product>(_adapter);
            //  return repository.GetAll();

            var query = from p in _context.Products
                        select p;

            return query.ToList();
        }

        /// <summary>
        /// Inserts the specified product.
        /// </summary>
        /// <param name="product">The product.</param>
        /// <returns></returns>
        public BO.Product Insert(BO.Product product)
        {
            BO.Product entity = null;
            if (product != null)
            {
                product.CreateDate = DateTime.Now;
                product.Active = true;
                entity = _context.Products.Add(product);
                _context.SaveChanges();
            }
            return entity;
        }

        /// <summary>
        /// Updates the specified product.
        /// </summary>
        /// <param name="product">The product.</param>
        /// <returns></returns>
        public int Update(BO.Product product)
        {
            int result = -1;
            if (product != null && product.ProductId.HasValue)
            {
                product.CreateDate = DateTime.Now;
                product.Active = true;
                var entity = _context.Products.FirstOrDefault(x => x.ProductId == product.ProductId);
                if (entity != null)
                {
                    entity.Name = product.Name;
                    entity.CategoryId = product.CategoryId;
                    entity.Price = product.Price;
                    entity.UpdateDate = DateTime.Now;
                }
                result = _context.SaveChanges();
            }
            return result;
        }

        /// <summary>
        /// Gets the by id.
        /// </summary>
        /// <param name="productId">The product id.</param>
        /// <returns></returns>
        public BO.Product GetById(int? productId)
        {
            BO.Product entity = null;
            //var products = _context.ImsDatabase.SqlQuery<BO.Product>("spProductGet @ProductId", new SqlParameter() { ParameterName = "ProductId", Value = productId });
            //if (products != null)
            //{
            //    entity = products.FirstOrDefault();
            //}

            entity = _context.Products.FirstOrDefault(x => x.ProductId == productId);

            return entity;
        }

        public int Delete(int? productId)
        {
            var entity = _context.Products.FirstOrDefault(x => x.ProductId == productId);
            if (entity != null)
            {
                _context.Products.Remove(entity);
                _context.SaveChanges();
                return 1;
            }
            return -1;
        }
    }
}